package com.sunday.aomicaichang.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.IntegralAdapter;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.Integral;
import com.sunday.aomicaichang.ui.MallActivity;
import com.sunday.aomicaichang.ui.common.WebViewActivity;
import com.sunday.member.utils.SignUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/4.
 */

public class MyIntegralActivity extends BaseActivity {
    @Bind(R.id.rule)
    TextView rule;
    @Bind(R.id.day)
    TextView day;
    @Bind(R.id.tv_balance)
    TextView tvBalance;
    @Bind(R.id.btn_rechange)
    TextView btnRechange;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private int pageSize = 20;
    private int pageNumber = 1;
    private BigDecimal score;
    private LinearLayoutManager layoutManager;
    private IntegralAdapter integralAdapter;
    private List<Integral.RecordsBean> recordsBeanList;
    private boolean isCanloadMore;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_integral);
        ButterKnife.bind(this);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        integralAdapter = new IntegralAdapter(mContext);
        recyclerView.setAdapter(integralAdapter);
        recyclerView.addOnScrollListener(mOnScrollListener);
        if (recordsBeanList == null) {
            recordsBeanList = new ArrayList<>();
        }
        getData();
    }
    @OnClick({R.id.rule,R.id.btn_rechange})
    void onClick(View v){
        switch (v.getId()){
            case R.id.rule:   intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", ApiClient.API_URL1+"sunday/mobile/advertisement/html5ByZoneCode?zoneKey=score_rule" );
                intent.putExtra("title","积分规则");
                startActivity(intent);
                break;
            case R.id.btn_rechange:
                intent=new Intent(mContext, MallActivity.class);
                startActivity(intent);
                break;
        }
    }
    private void getData() {
        String nonce = MD5Utils.random();
        String size = String.valueOf(pageSize);
        String number = String.valueOf(pageNumber);
        String[] str1 = new String[]{"type", "pageSize", "pageNumber", "nonce"};
        String[] str2 = new String[]{"2", size, number, nonce};
        showLoadingDialog(0);
        Call<ResultDO<Integral>> call = ApiClient.getApiAdapter().getIntegral(2, pageSize, pageNumber,
                nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO<Integral>>() {
            @Override
            public void onResponse(Call<ResultDO<Integral>> call, Response<ResultDO<Integral>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Integral> resultDO = response.body();
                if (resultDO.getCode() == 0) {
                    score = resultDO.getResult().getTotalScore();
                    tvBalance.setText(score + "");
                    day.setText(resultDO.getResult().getTotalSignDay() + "");
                    List<Integral.RecordsBean> records = resultDO.getResult().getRecords();
                    if (pageNumber==1){
                        recordsBeanList.clear();
                    }
                    recordsBeanList.addAll(records);
                    if (records.size()==10){
                        isCanloadMore=true;
                        pageNumber++;
                    }else {
                        isCanloadMore=false;
                    }
                    integralAdapter.setmData(recordsBeanList);
                    integralAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Integral>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {

        private int lastVisibleItem;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = layoutManager.findLastVisibleItemPosition();
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE
                    && lastVisibleItem + 1 == integralAdapter.getItemCount() && isCanloadMore) {
                //加载更多
                getData();
            }
        }
    };


}
